<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="root">

    </div>
</body>
<script src="https://unpkg.com/react@18.2.0/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18.2.0/umd/react-dom.development.js"></script>
<script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
<script type="text/babel">
    class App extends React.Component{
        state={
            num:0,
            age:19,
            sex:"男"
        }
        render(){
            console.log("渲染了")
            let {num,age,sex} = this.state;
            return (
                <div>
                   <h1>{num} --- {age} --- {sex}</h1>  
                   <button onClick={()=>{
                    // 在react18中setState 永远是一个异步的过程 无论你是否在react事件中事件都是异步的
                    setTimeout(()=>{
                        this.setState({
                            num:5
                        },()=>{
                            console.log("更新完了")
                        })
                        console.log(this.state.num)
                    })
                   }}>num+5</button> 
                   
                   <button onClick={()=>{
                    // 在react18中 无论是否在reac事件中，多个setState都会被合并更新。
                     setTimeout(()=>{
                        this.setState({
                            num:100
                        })
                        this.setState({
                            age:29
                        })
                        this.setState({
                            sex:"女"
                        })
                     })
                   }}>合并更新</button>
                </div>
            )
        }
    }

    let root = ReactDOM.createRoot(document.getElementById("root"))
    root.render(<App/>)
</script>
</html>